<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登陆页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">

</head>

<body>
    <div class="nav">
        <img src="pic/mowan.png" alt="">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
    </div>

    <div class="container-login">
        <div class="auth-tabs">
            <div class="tab-header">
                <button class="tab-btn active" onclick="switchTab('login')">登录</button>
                <button class="tab-btn" onclick="switchTab('register')">注册</button>
            </div>
            
            <!-- 登录框 -->
            <div id="login-tab" class="tab-content active">
                <div class="login-dialog">
                    <h3>登录</h3>
                    <div class="row">
                        <span>用户名</span>
                        <input type="text" name="login-username" id="login-username">
                    </div>
                    <div class="row">
                        <span>密码</span>
                        <input type="password" name="login-password" id="login-password">
                    </div>
                    <div class="row">
                        <button id="login-submit" onclick="login()">登录</button>
                    </div>
                </div>
            </div>
            
            <!-- 注册框 -->
            <div id="register-tab" class="tab-content">
                <div class="login-dialog">
                    <h3>注册</h3>
                    <div class="row">
                        <span>用户名</span>
                        <input type="text" name="register-username" id="register-username">
                    </div>
                    <div class="row">
                        <span>密码</span>
                        <input type="password" name="register-password" id="register-password">
                    </div>
                    <div class="row">
                        <span>确认密码</span>
                        <input type="password" name="confirm-password" id="confirm-password">
                    </div>
                    <div class="row">
                        <button id="register-submit" onclick="register()">注册</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        function switchTab(tabName) {
            // 移除所有激活状态
            document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
            document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
            
            // 激活选中的选项卡
            document.querySelector(`.tab-btn[onclick="switchTab('${tabName}')"]`).classList.add('active');
            document.getElementById(`${tabName}-tab`).classList.add('active');
        }

        function login() {
            const username = document.getElementById('login-username').value;
            const password = document.getElementById('login-password').value;
            
            if (!username || !password) {
                alert('请输入用户名和密码');
                return;
            }
            
            $.ajax({
                type:"post",
                url:"/user/login",
                contentType: "application/json",
                data: JSON.stringify({
                    userName: username,
                    password: password
                }),
                success:function(result){
                    if(result.code==200&&result.data!=null){
                        //用户登录成功
                        localStorage.setItem("loginUserId",result.data.userId);
                        localStorage.setItem("token",result.data.token);
                        location.href = "blog_list.html";
                    }else{
                        alert(result.errMsg);
                    }
                }
            });
        }

        function register() {
            const username = document.getElementById('register-username').value;
            const password = document.getElementById('register-password').value;
            const confirmPassword = document.getElementById('confirm-password').value;
            
            if (!username || !password || !confirmPassword) {
                alert('请填写所有字段');
                return;
            }
            
            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }
            
            if (password.length < 6) {
                alert('密码长度至少6位');
                return;
            }
            
            $.ajax({
                type:"post",
                url:"/user/register",
                contentType: "application/json",
                data: JSON.stringify({
                    userName: username,
                    password: password
                }),
                success:function(result){
                    if(result.code==200){
                        alert("注册成功！请登录");
                        // 注册成功后切换到登录选项卡
                        switchTab('login');
                        // 清空注册表单
                        document.getElementById('register-username').value = '';
                        document.getElementById('register-password').value = '';
                        document.getElementById('confirm-password').value = '';
                    }else{
                        alert(result.errMsg);
                    }
                }
            });
        }
    </script>
</body>

</html>